package com.wt.easyadmin.wx.service.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.PageHelper;
import com.wt.easyadmin.common.base.PageResultSet;
import com.wt.easyadmin.mapper.bar.TBarCashOutMapper;
import com.wt.easyadmin.mapper.bar.TBarConsumeMapper;
import com.wt.easyadmin.mapper.bar.TBarSettleMapper;
import com.wt.easyadmin.mapper.bar.TWxUserMapper;
import com.wt.easyadmin.model.bar.ProfitVo;
import com.wt.easyadmin.model.bar.TBarCashOut;
import com.wt.easyadmin.model.bar.TBarConsume;
import com.wt.easyadmin.model.bar.TBarSettle;
import com.wt.easyadmin.model.bar.TWxUser;
import com.wt.easyadmin.service.bussiness.CashOutService;
import com.wt.easyadmin.wx.WxUtils;
import com.wt.easyadmin.wx.entity.SettleInfo;
import com.wt.easyadmin.wx.entity.UserInfo;
import com.wt.easyadmin.wx.service.WxService;

import tk.mybatis.mapper.weekend.Weekend;
import tk.mybatis.mapper.weekend.WeekendCriteria;

@Service
public class WxServiceImpl implements WxService{
	protected Logger logger = LoggerFactory.getLogger(this.getClass());

	@Autowired 
	TWxUserMapper tWxUserMapper;
	@Autowired 
	TBarCashOutMapper tBarCashOutMapper;
	@Autowired 
	CashOutService cashOutService;
	@Autowired 
	TBarSettleMapper tBarSettleMapper;
	@Autowired
	TBarConsumeMapper tBarConsumeMapper;
	
	
	@Override
	public UserInfo getSessionByCode(String code,String reference) {
		String sessionByCode = WxUtils.getSessionByCode(code);
		String openid = JSONObject.parseObject(sessionByCode).getString("openid");
		String session_key = JSONObject.parseObject(sessionByCode).getString("session_key");
		UserInfo userInfo = new UserInfo(openid,session_key);
		TWxUser tWxUser = new TWxUser(openid);
		TWxUser selectOne = tWxUserMapper.selectOne(tWxUser);
		if(null!=selectOne) {
			BeanUtils.copyProperties(selectOne,userInfo);
			logger.info("selectOne:{}",JSONObject.toJSONString(selectOne));
			logger.info("tWxUser:{}",JSONObject.toJSONString(userInfo));
		}else {
			if(!StringUtils.isEmpty(reference)) {
				tWxUser.setReferenceId(reference);//绑定推荐人
			}
			tWxUserMapper.insert(tWxUser);
		}		
		return userInfo;
	}

	@Override
	public void updateUser(TWxUser tWxUser) {
		tWxUser.setUpdateTime(new Date());
		
		Weekend weekend = Weekend.of(TWxUser.class);
        WeekendCriteria<TWxUser, Object> criteria = weekend.weekendCriteria();
        criteria.andEqualTo(TWxUser::getOpenid,tWxUser.getOpenid());
		tWxUserMapper.updateByExampleSelective(tWxUser, weekend);
		
	}

	@Override
	public ProfitVo getProfit(String openid) {
		return tBarCashOutMapper.getProfit(openid);
	}

	@Override
	public PageResultSet<TBarCashOut> cashOutlistByUser(TBarCashOut tBarCashOut) {
		TWxUser tWxUser = new TWxUser(WxUtils.getOpenid());
		TWxUser selectOne = tWxUserMapper.selectOne(tWxUser);
		if(null!=selectOne&&!StringUtils.isEmpty(selectOne.getPhonenumber())) {
			tBarCashOut.setCashOutUser(selectOne.getPhonenumber());
			return cashOutService.findByCashOutUser(tBarCashOut);
		}else {
			//todo 绑定手机号提示
		}
		return null;
	}

	@Override
	public PageResultSet<SettleInfo> settlelistByUserAndStatus(SettleInfo settleInfo) {
		TWxUser tWxUser = new TWxUser(WxUtils.getOpenid());
		TWxUser selectOne3 = tWxUserMapper.selectOne(tWxUser);
		TWxUser selectOne2 = selectOne3;
		TWxUser selectOne = selectOne2;
		if(null!=selectOne&&!StringUtils.isEmpty(selectOne.getPhonenumber())) {
			PageHelper.offsetPage(settleInfo.getOffset(), settleInfo.getLimit());
	        if(!StringUtils.isEmpty(settleInfo.getOrderBy())) {
	            PageHelper.orderBy(settleInfo.getOrderBy());
	        }      
	        Weekend<TBarSettle> weekend = Weekend.of(TBarSettle.class);
	        WeekendCriteria<TBarSettle, Object> criteria = weekend.weekendCriteria();
	        if (!StringUtils.isEmpty(selectOne.getPhonenumber())) {
	        	criteria.andEqualTo(TBarSettle::getSettleUser,selectOne.getPhonenumber());     
	        	criteria.andEqualTo(TBarSettle::getIsSettle,settleInfo.getSettleStatus());
	        }
	        PageResultSet<SettleInfo> resultSet = new PageResultSet<>();	
	        List<TBarSettle> list = tBarSettleMapper.selectByExample(weekend);
	        long count = tBarSettleMapper.selectCountByExample(weekend);
	        List<SettleInfo> settleInfos = new ArrayList<>();
	        if(list!=null&&list.size()>0) {
	        	for (TBarSettle t : list) {
	        		SettleInfo s = new SettleInfo();
	        		s.setSettleAmount(t.getSettleAmount());//结算金额
	        		s.setSettleTime(t.getCreateTime());
	        		TBarConsume c = tBarConsumeMapper.selectByPrimaryKey(t.getConsumeId());
	        		TWxUser tWxUser2 = new TWxUser();
	        		tWxUser2.setPhonenumber(c.getPhonenumber());
	        		tWxUser2 =  tWxUserMapper.selectOne(tWxUser);
	        		s.setNickName(tWxUser2.getNickName());
	        		s.setAvatarUrl(tWxUser2.getAvatarUrl());
	        		settleInfos.add(s);
				}
	        }
	        resultSet.setRows(settleInfos);
	        resultSet.setTotal(count);		
	        return resultSet;
			
		}else {
			//todo 绑定手机号提示
		}
		return null;
	}

}
